﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Fast.Extensions.DatabaseAccessor.EntityFrameworkCore
{
    public static class PagedQueryableExtensions
    {
        /// <summary>
        /// 分页扩展
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="orderBy"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sorting"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static PagedList<TEntity> ToPagedList<TEntity, TProperty>(this IQueryable<TEntity> queryable, Expression<Func<TEntity, TProperty>> orderBy, int pageIndex = 1, int pageSize = 20, Sorting sorting = Sorting.Descending)
                where TEntity : new()
        {
            if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的整数");

            queryable = sorting == Sorting.Ascending ? queryable.OrderBy(orderBy) : queryable.OrderByDescending(orderBy);

            var totalCount = queryable.Count();
            var items = queryable.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);

            return new PagedList<TEntity>
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Items = items,
                TotalCount = totalCount,
                TotalPages = totalPages,
                HasNextPages = pageIndex < totalPages,
                HasPrevPages = pageIndex > 1
            };
        }

        /// <summary>
        /// 分页扩展
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="orderBy"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sorting"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static async Task<PagedList<TEntity>> ToPagedListAsync<TEntity, TProperty>(this IQueryable<TEntity> queryable, Expression<Func<TEntity, TProperty>> orderBy, int pageIndex = 1, int pageSize = 20, Sorting sorting = Sorting.Descending, CancellationToken cancellationToken = default)
            where TEntity : new()
        {
            if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的整数");

            queryable = sorting == Sorting.Ascending ? queryable.OrderBy(orderBy) : queryable.OrderByDescending(orderBy);

            var totalCount = await queryable.CountAsync(cancellationToken);
            var items = await queryable.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken);
            var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);

            return new PagedList<TEntity>
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Items = items,
                TotalCount = totalCount,
                TotalPages = totalPages,
                HasNextPages = pageIndex < totalPages,
                HasPrevPages = pageIndex > 1
            };
        }
    }
}
